# -*- coding: UTF-8 -*-


from .x2mbase import X2mBase
from .editmodel import EditX2m
import copy


class X2mForm(X2mBase):
    def __init__(self, field_info,  **kwargs):
        super().__init__(field_info, type='form', **kwargs)
        # self.parentData = {}
        self.edit_model = None

    def read(self, res_id):
        Model = self.Model
        fields = self.fields_list
        res = Model.read(res_id, fields=fields)
        return res[0]

    def _edit_model_get(self, record=None, values=None, parentData=None):
        return EditX2m(viewmodel=self, record=record, values=values, parentData=parentData)

    def context_get(self, parentData):
        context_fn = self.field_info.get('context')
        # print('context_fn,', context_fn)
        prt = self.parent
        context = prt.context
        # print('context,', context)
        ctx = {}
        ctx.update(context)
        ctx.update(context_fn)
        return ctx

    def onchange_new(self, parentData):
        self.edit_model = self._edit_model_get()
        return self.edit_model.onchange_new(parentData)

    def onchange(self, fname, value):
        return self.edit_model.onchange(fname, value)

    def commit(self):
        return self.edit_model.commit()
